package com.yuexunit.operationman.nbsy.module.nurse.ui;

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import com.yuexunit.operationman.nbsy.R;
import com.yuexunit.operationman.nbsy.base.BaseKJActivity;
import com.yuexunit.operationman.nbsy.base.ExitApplication;
import com.yuexunit.operationman.nbsy.base.SubmitBean;
import com.yuexunit.operationman.nbsy.config.AppConfig;
import com.yuexunit.operationman.nbsy.module.nurse.Bean.NurseBean;
import com.yuexunit.operationman.nbsy.module.nurse.model.impl.NurseModel;
import com.yuexunit.operationman.nbsy.module.nurse.presenter.INursePresenter;
import com.yuexunit.operationman.nbsy.module.nurse.presenter.impl.NursePresenter;
import com.yuexunit.operationman.nbsy.module.nurse.ui.view.INurseView;
import com.yuexunit.operationman.nbsy.widget.LoadingDialog;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2017/7/7 0007.
 */

public class Act_Ops_Other extends BaseKJActivity implements INurseView {
    @Bind(R.id.tv_patient_name)
    TextView tvPatientName;
    @Bind(R.id.tv_patient_sex)
    TextView tvPatientSex;
    @Bind(R.id.tv_patient_visitno)
    TextView tvPatientVisitno;
    @Bind(R.id.tv_patient_disease)
    TextView tvPatientDisease;
    @Bind(R.id.edit_txt)
    EditText editTxt;

    LoadingDialog loadingDialog;
    INursePresenter presenter;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_ops_other);
        ButterKnife.bind(this);
        ExitApplication.getInstance().addActivity(this);
    }

    @Override
    public void initData() {
        super.initData();
        loadingDialog = new LoadingDialog(this);
        presenter = new NursePresenter(this,new NurseModel());
        setTitle();
        setContent();
    }

    private void setTitle()
    {
        tvPatientName.setText(AppConfig.NURSEBEAN.getObject().getNursing().getPatientName());
        if(AppConfig.NURSEBEAN.getObject().getNursing().getPatientSex().equals("1")) {
            tvPatientSex.setText("男");
        }else if(AppConfig.NURSEBEAN.getObject().getNursing().getPatientSex().equals("2"))
        {
            tvPatientSex.setText("女");
        }
        else {
            tvPatientSex.setText("不详");
        }
        tvPatientVisitno.setText("住院号" + AppConfig.NURSEBEAN.getObject().getNursing().getInHospitalNo());
        tvPatientDisease.setText(AppConfig.NURSEBEAN.getObject().getNursing().getOperationName());
    }

    private void setContent() {
        for (NurseBean.ObjectEntity.NursingItemListEntity p : AppConfig.NURSEBEAN.getObject().getNursingItemList()) {
            switch (p.getItemCode()) {
                case "Other":
                    editTxt.setText(p.getItemValue());
                    break;
            }
        }
    }

    @OnClick({R.id.titlebar_docadvice_img_back, R.id.tv_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.titlebar_docadvice_img_back:
                finish();
                break;
            case R.id.tv_submit:
                insertContent();
                break;
        }
    }

    private void insertContent()
    {
        for(int i = 0;i<AppConfig.NURSEBEAN.getObject().getNursingItemList().size();i++) {
            String value = "";
            switch (AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).getItemCode())
            {
                case "Other":
                    AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue(editTxt.getText().toString());
                    break;
            }
        }

        if(AppConfig.NURSEBEAN.getObject().getNursing().getNursingId() == null) {
            Map<String, String> params = new HashMap<>();
            params.put("operationNo", PreferenceHelper.readString(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_OPERATION_ID)+"");
            params.put("saveData", JSON.toJSONString(AppConfig.NURSEBEAN));
            presenter.insertNurseDate(params);
        }
        else {
            Map<String, String> params = new HashMap<>();
            params.put("accountId", PreferenceHelper.readInt(aty, AppConfig.USER_INFO, AppConfig.USER_ACCOUNT) + "");
            params.put("fullName", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.USER_FULLNAME) + "");
            params.put("saveData", JSON.toJSONString(AppConfig.NURSEBEAN));
            presenter.updateNurseDate(params);
        }
    }

    @Override
    public void insertNurseDate(SubmitBean submitBean) {
        if(submitBean.getCode() == 200)
        {
            ViewInject.toast("保存成功");
            finish();
        }
        else {
            ViewInject.toast(submitBean.getDesc());
        }
    }

    @Override
    public void updateNurseDate(SubmitBean submitBean) {
        if(submitBean.getCode() == 200)
        {
            ViewInject.toast("保存成功");
            finish();
        }
        else {
            ViewInject.toast(submitBean.getDesc());
        }
    }

    @Override
    public void loadingDialogStatus(int status) {
        switch (status) {
            case AppConfig.SHOW_DIALOG:
                loadingDialog.setCanceledOnTouchOutside(false);
                loadingDialog.show();
                break;
            case AppConfig.DISMISS_DIALOG:
                loadingDialog.dismiss();
                break;

        }
    }
}
